// pages/merchants/huifu/huifu.js
let uploadImage = require('../../../utils/uploadFile.js');


var App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    releaseFocus: true,
    leixing: [],
    imgList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      ids: options.id
    })
    //  举报信息
    App.HttpService.jbxx().then(res => {
      console.log(res)
      this.setData({
        leixing: res.data.data
      })
    })
    this.huoqu();

    // 预览
  },
  // 显示举报框
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },

  // 获取主题回复内容

  huoqu: function() {

    var params = {
      reviewId: this.data.ids,
      buyerId: wx.getStorageSync('userId')
    }
    //获取回复信息
    App.HttpService.qqplhf(params).then(res => {
      console.log(res)
     
      this.setData({
        releaseFocus: !res.data.data.isShield,
        huifu: res.data.data.list,
        yuan: res.data.data.original
      })
    })
  },




  // 关闭举报框
  hideModal(e) {
    this.setData({
      modalName: null
    })
    var s = e.currentTarget.dataset.s
    console.log(s)

    var params = {
      tousuedPersonId: this.data.yuan.review_person,
      tousuPersonId: wx.getStorageSync('userId'),
      type: s
    }

    //  举报请求

    App.HttpService.tousu(params).then(res => {
      console.log(res)

    })



    if (s) {
      wx.showToast({
        title: '举报成功',
        duration: 1500,
      })

    } else {
      wx.showToast({
        title: '举报失败',
        icon: 'none',
        duration: 1000,
      })
      console.log("失败")
    }

  },
  // 预览图片
  yulan: function(e) {

    var a = e.currentTarget.dataset.tu
    wx.previewImage({
      current: a, // 当前显示图片的http链接
      urls: [a]
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },


  /**
  * 点击回复
      releaseFocus: false,
  */
  bindReply: function(e) {
    this.setData({
      releaseFocus: true
    })
  },


  // 点赞
  huifudianz: function(e) {
    console.log(e)
    // g改变单条里的数据
    var index = e.currentTarget.dataset.idx
    let arr = `huifu[${index}].isSupport`;
    // 详情案例
    // let completeStatus = `todoLists[${index}].completeStatus`;
    this.setData({
      [arr]: !this.data.huifu[index].isSupport,
    })
    var params = {
      reviewId: this.data.huifu[index].id,
      buyerId: wx.getStorageSync('userId')
    }
    // 请求点赞的借口
    App.HttpService.pldz(params).then(res => {
      console.log(res)
      if (res.data.data) {
        let shu = `huifu[${index}].supportCount`;
        this.setData({
          [shu]: this.data.huifu[index].supportCount + 1,
        })
        wx.showToast({
          title: '点赞成功',
          icon: 'success',
          duration: 800
        })
      } else {
        let shu = `huifu[${index}].supportCount`;
        this.setData({
          [shu]: this.data.huifu[index].supportCount - 1,
        })
        wx.showToast({
          title: '取消成功',
          icon: 'success',
          duration: 800
        })
      }
    })
  },
  // 点赞
  huifudianzs: function(e) {
    console.log(e)
    // g改变单条里的数据

    var index = e.currentTarget.dataset.idx
    let arr = `yuan.isSupport`;
    // 详情案例
    // let completeStatus = `todoLists[${index}].completeStatus`;
    this.setData({
      [arr]: !this.data.yuan.isSupport,
    })
    var params = {
      reviewId: this.data.yuan.id,
      buyerId: wx.getStorageSync('userId')
    }
    // 请求点赞的借口
    App.HttpService.pldz(params).then(res => {
      console.log(res)
      if (res.data.data) {
        let shu = `yuan.supportCount`;
        this.setData({
          [shu]: this.data.yuan.supportCount + 1,
        })
        wx.showToast({
          title: '点赞成功',
          icon: 'success',
          duration: 800
        })
      } else {
        let shu = `yuan.supportCount`;
        this.setData({
          [shu]: this.data.yuan.supportCount - 1,
        })
        wx.showToast({
          title: '取消成功',
          icon: 'success',
          duration: 800
        })
      }
    })
  },



  // 图片上传
  ChooseImage() {
    var _this = this
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        var tempFilePaths = res.tempFilePaths;
        this.upload(tempFilePaths)
      }
    });
  },

  // 调用封装的上传图片的写法

  upload(img) {

    // 图片上传oss
    let that = this
    let {
      imgList
    } = that.data
    for (let i = 0; i < img.length; i++) {
      wx.showLoading({
        title: '上传中' + (i + 1) + '/' + img.length,
        mask: true
      })
      wx.hideLoading()
      uploadImage(img[i], 'weapp/', function(result) { // weapp/ 上传文件夹
        imgList.push(result)
        that.setData({
          imgList
        })
        wx.hideLoading()
      }, function(result) {
        wx.hideLoading()
      })
    }
  },

  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '温馨提示',
      content: '确认删除图片',
      cancelText: '拒绝',
      confirmText: '确认',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList,
            tupian: ""
          })
        }

      }
    })
  },
  // 三元运算符有图片的时候
  ChooseImages() {

  },
  //点击发送
  fasong: function() {
    var params = {
      // 文章ID 或者评论id
      articleId: this.data.yuan.id,
      // 留言内容
      content: this.data.neirong,
      // 评论的类型 
      type: 1,
      // 当前评论人的id
      reviewPerson: wx.getStorageSync('userId'),
      // 图片路径
      path: this.data.imgList[0],
    }
    if (params.content || params.path) {

      if (params.content) {
        var paramss = {
          text: params.content
        }
        App.HttpService.jiancem(paramss).then(res=>{
          console.log("敏感反馈",res)
          var b = res.data.data
          
          if(b){
            App.HttpService.pingluns(params).then(res => {
              console.log(res)
              if (res.data.code == 200) {
                console.log("评论成功")
                wx.showToast({
                  title: '评论成功',
                  icon: 'success',
                  duration: 800
                })
                this.setData({
                  neirong: "",
                  imgList: [],
                })
                this.huoqu();
              } else {
                wx.showToast({
                  title: '评论失败',
                  icon: 'loading',
                  duration: 800
                })
                console.log("评论失败")
              }
            });

          }else{
            wx.showToast({
              title: '当前评论含有敏感词汇',
              icon: 'none',
              duration: 1800
            })
            return

          }
        })

      }else{

        App.HttpService.pingluns(params).then(res => {
          console.log(res)
          if (res.data.code == 200) {
            console.log("评论成功")
            wx.showToast({
              title: '评论成功',
              icon: 'success',
              duration: 800
            })
            this.setData({
              neirong: "",
              imgList: [],
            })
            this.huoqu();
          } else {
            wx.showToast({
              title: '评论失败',
              icon: 'loading',
              duration: 800
            })
            console.log("评论失败")
          }
        });





      }

    } else {
      wx.showToast({
        title: '内容不能为空',
        icon: 'loading',
        duration: 1000
      })
      return
    }
  },

  // 获取内容
  neir: function(e) {
    console.log(e.detail.value)
    this.setData({
      neirong: e.detail.value
    })

  },
})